package cn.go.bus;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import cn.go.buster.launcher.api.GoBuster;

/**
 * @author logcat
 */
public class ThirdActivity extends AppCompatActivity {


    private TabLayout tbThird;
    private ViewPager vpThird;

    private List<Fragment> pageList;
    private TestPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        tbThird = findViewById(R.id.tbThird);
        vpThird = findViewById(R.id.vpThird);


        pageList = new ArrayList<>();
        pageList.add(new TestFragment());
        pageList.add(new TestFragment2());

        pagerAdapter = new TestPagerAdapter(getSupportFragmentManager() , FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT , pageList);
        vpThird.setAdapter(pagerAdapter);
        pagerAdapter.notifyDataSetChanged();
        //要渲染几个fragment
        vpThird.setOffscreenPageLimit(3);
        //添加一个 页面滑动 的监听，跟tabLayout联动
        vpThird.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tbThird));
        tbThird.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vpThird.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void logPrint(View view) {
        GoBuster.getInstance().post("tag3");
    }
}